package com.zzedu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzedu.domain.ZzExerciseBookSubmitContent;
import com.zzedu.dto.StudentExerciseReportDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 作业内容Mapper接口
 *
 * @author Mr.Wang
 * @date 2025-06-02
 */
public interface ZzExerciseBookSubmitContentMapper extends BaseMapper<ZzExerciseBookSubmitContent> {


    @Select({
            "<script>",
            "SELECT",
            "    a.student_id AS studentId,",
            "    COUNT(*) AS totalCount,",
            "    sum(submit_count) as submitCount,",
            "    SUM(CASE WHEN a.correct_count > 0 THEN 1 ELSE 0 END) AS correctCount",
            "FROM zz_exercise_book a",
            "WHERE a.deleted = 0",
//            "  AND a.clazz_id = #{clazzId}",
            "  AND a.picture_book_id = #{pictureBookId}",
            "<if test='stuIdList != null and stuIdList.size() > 0'> ",
            "AND a.student_id IN ",
            "<foreach item='id' collection='stuIdList' open='(' separator=',' close=')'> ",
            "#{id} ",
            "</foreach> ",
            "</if> ",
            "GROUP BY a.student_id",
            "</script>"
    })
    @Results({
            @Result(property = "studentId", column = "studentId"),
            @Result(property = "totalCount", column = "totalCount"),
            @Result(property = "submitCount", column = "submitCount"),
            @Result(property = "correctCount", column = "correctCount")
    })
    List<StudentExerciseReportDTO> getStudentExerciseReport(
            @Param("clazzId") Long clazzId,
            @Param("pictureBookId") Long pictureBookId,
            @Param("stuIdList") List<Long> stuIdList
    );
}
